package com.infore.exchange.main.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.infore.common.base.EppPage;
import com.infore.common.base.Msg;
import com.infore.common.base.ResultUtil;
import com.infore.common.base.WrapperHelp;
import com.infore.common.fw.BaseController;
import com.infore.common.model.entity.exchange.Services;
import com.infore.common.model.entity.exchange.vo.ServicesVo;
import com.infore.common.response.ApiListResponse;
import com.infore.exchange.main.service.ServicesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * Hadoop集群服务 前端控制器
 * </p>
 *
 * @author lijh
 * @since 2023-05-15
 */
@RestController
@Api(value = "Hadoop集群服务前端控制器", tags = "Hadoop集群服务前端控制器")
@RequestMapping("/services")
public class ServicesController extends BaseController {
    private String prefix = "/services";

    @Autowired
    private ServicesService ServicesService;

    @GetMapping()
    public String ServicesController() {
        return prefix;
    }

    /**
     * 查询列表
     *
     * @param servicesVo 的vo类
     * @return Msg<Object> 结果集
     * @author lijh
     * @since 2023-05-15
     */
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation(value = "查询列表", notes = "列表查询 ")
    public Msg<Object> list(ServicesVo servicesVo) {
        //sql查询条件
        Wrapper<Services> wrapper = null;
        try {
            wrapper = (Wrapper<Services>) WrapperHelp.addCondition(new EntityWrapper<Services>(), servicesVo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage());
        }
        //查询方法
        Page<Services> eppPage = new EppPage<Services>(ServicesService, servicesVo, wrapper);
        return ResultUtil.successPage(ServicesService.selectPage(eppPage, wrapper));
    }

    /**
     * 查询列表
     *
     * @param services
     * @return Msg<Object> 结果集
     * @author lijh
     * @since 2023-05-15
     */
    @ResponseBody
    @ApiOperation(value = "添加", notes = "新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Msg<Object> add(@RequestBody Services services) {
        Wrapper<Services> wrapper = new EntityWrapper<Services>();
        wrapper.eq("1", "1");
        List<Services> entityList = ServicesService.selectList(wrapper);
        if (entityList.size() > 0) {
            return ResultUtil.error("重复添加");
        }
        //插入数据
        boolean flag = ServicesService.insert(services);
        return ResultUtil.setResult(flag, services.getServiceId().toString());
    }


    /**
     * 更新方法
     *
     * @param services
     * @return Msg<Object>
     * @author lijh
     * @since 2023-05-15
     */
    @ResponseBody
    @ApiOperation(value = "更新", notes = "更新")
    @RequestMapping(value = "/edit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Msg<Object> edit(@RequestBody Services services) {
        //插入数据
        boolean flag = ServicesService.updateById(services);
        return ResultUtil.setResult(flag, services.getServiceId().toString());
    }

    /**
     * 删除
     *
     * @param serviceId
     * @return Msg<Object>
     * @author lijh
     * @since 2023-05-15
     */
    @RequestMapping(value = "/del", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "删除", notes = "删除")
    public Msg<Object> del(String serviceId) {

        Map<String, Object> columnMap = new HashMap<String, Object>();
        columnMap.put("SERVICE_ID", serviceId);
        boolean flag = ServicesService.deleteByMap(columnMap);
        return ResultUtil.setResult(flag);
    }

    @RequestMapping(value = "/queryServiceProcess", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "集群服务进程", notes = "集群服务进程")
    public ApiListResponse<Map<String, Object>> queryServiceProcess() {
        List<Map<String, Object>> list = ServicesService.queryServiceProcess();
        return ApiListResponse.ok(list);
    }
}
